/**
 * Created by jiaaobo on 16/11/29.
 */

import Banner from "../../../common/component/Banner";
import Util from "util";
import { MessageBox } from 'mint-ui';
import API  from "api";
import CONST from  "const";
import CDSev from "../../../common/util/seckill";
let storeImgRadio = 2.25;
let currentTimerObj;

export default{
  name: "orderMain",
  data(){
    return {
      imgList: [],
      lastOrder: "",
      isAllowOpenDoor: false,
      seckillMsg : ''
    }

  },

  computed: {
    getStarTime: function () {
      return new Date(this.lastOrder.startTime).Format('MM月dd日 hh:ss');
    },

    getEndTime: function () {
      return new Date(this.lastOrder.endTime).Format('hh:ss');
    },


    showOpenDoorText : function(){
      switch (this.lastOrder.type) {
        //未开始
        case CONST.BILL_STATUS.NOT :
          return "立即开门";
          break;
        case CONST.BILL_STATUS.RUNNING :
          return "提前结束并开门";
          break;
        case CONST.BILL_STATUS.END :
          return "结束并开门";
          break;
        default :
          break;
      }
    },


    showMessage: function () {
      switch (this.lastOrder.type) {
        //未开始
        case CONST.BILL_STATUS.NOT :
          if(this.isAllowOpenDoor){
            return '您的预约时间已到';
          }
          else{
            return '距离您的预约时间还有';
          }
          break;
        case CONST.BILL_STATUS.RUNNING :
          this.isAllowOpenDoor = true;
          return "您的vii之旅已进行";
          break;
        case CONST.BILL_STATUS.END :
          this.isAllowOpenDoor = true;
          return "您的vii之旅已进行";
          break;
        default :
          break;
      }
    }
  },

  mounted: function () {
    console.log("orderMain 初始化..");

    /**
     * 查询顶部banner
     */
    API.biz.getBannerListByType("INDEX_PAGE")
      .then((res)=> {
        this.imgList = res.bizData.map((banner)=> {
          banner.url = banner.picUrl;
          banner.link = banner.pageUrl;
          return banner;

        });
      });

    /**
     * 查询最后一次有效预约
     */
    API.biz.queryUserAppointInfo()
      .then((res)=> {
        console.log(res);

        this.lastOrder = res.bizData;
        this.lastOrder.netTime =  res.ts;
        // this.lastOrder.type = 1;
        let time = this.lastOrder.startTime - this.lastOrder.netTime;
        if(time > 0){
            this.isAllowOpenDoor = false;
          //倒计时
          //创建一个时间timer
          currentTimerObj = CDSev.createTimer(this.lastOrder.startTime,(date)=>{
            this.seckillMsg = date;
          },(date)=>{
            console.log("结束...");
            this.lastOrder.type = CONST.BILL_STATUS.NOT;
            this.isAllowOpenDoor = true;
            this.seckillMsg = "";
          },"detailTimer",res.ts);
        }
        else{
          this.isAllowOpenDoor = true;
        }
      });
  },

  beforeDestroy : function(){

    if(currentTimerObj)
        CDSev.clearTimer(currentTimerObj.id);
  },

  methods: {

    addOrder : function(){
      window.getEventBus().$emit("tabSelected",'tab-order');
    },

    isShowTemporaryDoor : function(t){
        let time = new Date().getTime() - this.lastOrder.endTime;
        if(t == CONST.BILL_STATUS.RUNNING && time <  0){
           return true;
        }
      return false;
    },

    toggleDoor: function () {
      console.log("toggle door");
      getRouter().push({name: 'orderResult', params: {}});
    },

    //开门
    onOpenDoor : function(btnType){
      let params = {
        appointId : this.lastOrder.appointId
      }

      //健身中
      if(!btnType &&  this.lastOrder.type == CONST.BILL_STATUS.RUNNING){
        params.type = CONST.BILL_STATUS.END;
        MessageBox.confirm('确定提前结束您的Vii之旅?').then(action => {
          API.biz.openTheDoor(params,{hasLoading : true})
            .then((res)=>{
              Util.Storage.setSgObj("orderResult",res.bizData);
              window.getRouter().push({path : "/order/orderResult"});
            });
        });
      }
      //首次开门
      else{
        params.type = this.lastOrder.type || 0;
        API.biz.openTheDoor(params,{hasLoading : true})
          .then((res)=>{
            Util.Storage.setSgObj("orderResult",res.bizData);
            window.getRouter().push({path : "/order/orderResult"});
          });
      }

    }

  },

  components: {
    Banner
  }
}
